addEvent("cmta_panelgracza:getplayerinfo", true)
addEventHandler("cmta_panelgracza:getplayerinfo", resourceRoot, function()
	local result=exports["cmta-db"]:dbGet("SELECT premium,limitveh FROM cmta_users WHERE id=?", getElementData(client,"player:data").uid)
	local result2=exports["cmta-db"]:dbGet("SELECT * FROM cmta_pojazdy WHERE ownedPlayer=?", getElementData(client,"player:data").uid)
	liczba = 0
	if result2 and #result2 > 0 then
	for i,v in pairs(result2) do
	liczba = i
	end
    end
	triggerClientEvent(client, "cmta_panelgracza:playerinfo", resourceRoot, result[1]["premium"],result[1]["limitveh"],liczba)
end)

addEvent("cmta_panelgracza:getachievements", true)
addEventHandler("cmta_panelgracza:getachievements", resourceRoot, function()
--	local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_achievements WHERE uid=?", getElementData(client,"player:uid"))
   -- triggerClientEvent(client, "cmta_panelgracza:achievements", resourceRoot, result)
	local query = exports["cmta-db"]:dbGet("SELECT * FROM `cmta_users` WHERE `id`=?",  getElementData(client,"player:data").uid)
    triggerClientEvent(client, "cmta_panelgracza:achievements", resourceRoot, query)

end)

addEvent("cmta_panelgracza:getvehicles",true)
addEventHandler("cmta_panelgracza:getvehicles",resourceRoot,function()
local result=exports["cmta-db"]:dbGet("select * from cmta_pojazdy WHERE ownedPlayer=?",getElementData(client,"player:data").uid)
if result and #result >= 1 then
triggerClientEvent(client, "cmta_panelgracza:vehicles", resourceRoot,result)
end
end)

addEvent("cmta_panelgracza:getvehinfo", true)
addEventHandler("cmta_panelgracza:getvehinfo", resourceRoot, function(id)
	local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_pojazdy WHERE id=?", id)
    triggerClientEvent(client, "cmta_panelgracza:vehinfo", resourceRoot, result,id)
end)

addEvent("cmta_panelgracza:listawspol", true)
addEventHandler("cmta_panelgracza:listawspol", resourceRoot, function(id)
local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_pojazdy WHERE id=?",tonumber(id))
local wspol = split(result[1]['rent'], ',')
local twspol={}
local nickiwspol={}
for i,v in pairs(wspol) do
if v == "0" then return end
local plr=exports["cmta-db"]:dbGet("SELECT * FROM cmta_users WHERE id=?",v)
table.insert(twspol,v)
table.insert(nickiwspol,{v,plr[1]["login"]})
end
triggerClientEvent(client, "cmta_panelgracza:listakluczy", resourceRoot,nickiwspol,twspol)
end)

addEvent("cmta_panelgracza:zabierzkluczyki", true)
addEventHandler("cmta_panelgracza:zabierzkluczyki", resourceRoot, function(uid,tabela,id)
local check=exports["cmta-db"]:dbGet("SELECT * FROM cmta_pojazdy WHERE id=?",id)
if check[1]["ownedPlayer"] ~= getElementData(client,"player:data").uid then
return end
table.remove(tabela,uid)
for i,v in ipairs ( getElementsByType ( "vehicle" )) do
	if getElementData(v,"vehicle:data").id == tonumber(id) then
	local data = getElementData(v,"vehicle:data")
	data.rent = tabela
	setElementData(v,"vehicle:data",data)
	end
end
local result = exports["cmta-db"]:dbSet("update cmta_pojazdy set rent=? where id=?",string.format("%s", table.concat(tabela, ',') ),id)
if result then
triggerClientEvent(client, "cmta_panelgracza:refreshlista", resourceRoot,id)
end
end)


addEvent("cmta_panelgracza:dodaj_wypozyczenie", true)
addEventHandler("cmta_panelgracza:dodaj_wypozyczenie", resourceRoot, function(dane,tabela,id)
local check=exports["cmta-db"]:dbGet("SELECT * FROM cmta_pojazdy WHERE id=?",id)
if check[1]["ownedPlayer"] ~= getElementData(client,"player:data").uid then
	exports["PSSM-notyfikacje"]:pssmnoti2("#ff0000Ten pojazd nie należy do Ciebie.",client)
return end
if dane == getElementData(client,"player:data").uid then return end
if (type(tabela) ~= "table") then tabela={} end
local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_users WHERE id=?",dane)
if result and #result > 0 then
for i,v in pairs(tabela) do
if tonumber(v) == tonumber(dane) then exports["PSSM-notyfikacje"]:pssmnoti2("#ff0000Ten gracz ma klucze do tego samochodu.",client) return end
end
table.insert(tabela,dane)
for i,v in ipairs ( getElementsByType ( "vehicle" )) do
	if getElementData(v,"vehicle:data").id == tonumber(id) then
		local data = getElementData(v,"vehicle:data")
		data.rent = tabela
		setElementData(v,"vehicle:data",data)
	end
end
exports["cmta-db"]:dbSet("update cmta_pojazdy set rent=? where id=?",string.format("%s", table.concat(tabela, ',') ),id)
--exports["PSSM-notyfikacje"]:pssmnoti2("#00ff00Pomyślnie przekazano "..result[1]["login"].." kluczyki do samochodu.",client)
	triggerClientEvent(client, "pssm_panelgracza:refreshlista", resourceRoot,id)
	return
end
local result2 = exports["cmta-db"]:dbGet("SELECT * FROM cmta_users WHERE login=?",dane)
if result2 and #result2 > 0 then
	for i,v in pairs(tabela) do
	if v == dane then exports["PSSM-notyfikacje"]:pssmnoti2("#ff0000Ten gracz ma klucze do tego samochodu.",client) return end
	end
	table.insert(tabela,result2[1]["id"])
	for i,v in ipairs ( getElementsByType ( "vehicle" )) do
		if getElementData(v,"vehicle:data").id == tonumber(id) then
			local data = getElementData(v,"vehicle:data")
			data.rent = tabela
			setElementData(v,"vehicle:data",data)
		end
	end
	exports["cmta-db"]:dbSet("update cmta_pojazdy set rent=? where id=?",string.format("%s", table.concat(tabela, ',') ),id)
    exports["PSSM-notyfikacje"]:pssmnoti2("#00ff00Pomyślnie przekazano "..result2[1]["login"].." kluczyki do samochodu.",client)
		triggerClientEvent(client, "pssm_panelgracza:refreshlista", resourceRoot,id)
        return
end
exports["PSSM-notyfikacje"]:pssmnoti2("#ff0000Nie odnaleziono gracza o podanych danych.",client)
end)

addEvent("cmta_panelgracza:getacts", true)
addEventHandler("cmta_panelgracza:getacts", resourceRoot, function()
	local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_acts WHERE uid=?", getElementData(client,"player:data").uid)
    triggerClientEvent(client, "cmta_panelgracza:acts", resourceRoot, result)
end)

addEvent("cmta_panelgracza:pobierzhistorie",true)
addEventHandler("cmta_panelgracza:pobierzhistorie",resourceRoot,function(id)
local result=exports["cmta-db"]:dbGet("select * from cmta_logs_vehicles WHERE vehid=?",id)
if result and #result >= 1 then
triggerClientEvent(client, "cmta_panelgracza:historia", resourceRoot,result)
end
end)

addEvent("cmta_panelgracza:getinventory", true)
addEventHandler("cmta_panelgracza:getinventory", resourceRoot, function()
	local result=exports["cmta-db"]:dbGet("SELECT * FROM cmta_inventory WHERE uid=?", getElementData(client,"player:data").uid)
    triggerClientEvent(client, "cmta_panelgracza:inventory", resourceRoot, result)
end)

addEvent("cmta_panelgracza:ekwipunek_wyrzuc", true)
addEventHandler("cmta_panelgracza:ekwipunek_wyrzuc", resourceRoot, function(typ,amount)
	if typ == "papierosy" then
		exports["cmta-db"]:dbSet("update cmta_inventory set cigarettes=cigarettes-? where uid=?",amount,getElementData(client,"player:data").uid)
    end
	local x,y,z = getElementPosition(client)
	giftinv = createPickup(x-2,y,z-0.8,3,1210)
	setElementData(giftinv,"kickeditem",true)
	setElementData(giftinv,"name",typ)
	setElementData(giftinv,"amount",amount)
	outputChatBox("#808080Wyrzucono #ff0000"..amount.."x "..typ.."",client,255,255,255,true)
end)

addEvent("cmta_panelgracza:bonuscode", true)
addEventHandler("cmta_panelgracza:bonuscode", resourceRoot, function(code)
	local result=exports["cmta-db"]:dbGet("select * from cmta_bonuscodes WHERE code=?",code)
if result and #result > 0 then
	if result[1]["points"] > 0 then
		outputChatBox("punkty",client)
	end
	if result[1]["exp"] > 0 then
		outputChatBox("expa",client)
	end
	if result[1]["vip"] > 0 then
		outputChatBox("vipa",client)
	end
	if result[1]["car"] > 0 then
		outputChatBox("auto",client)
	end	
	exports["cmta-db"]:dbSet("DELETE FROM cmta_bonuscodes WHERE code=?",code)
else
end
end)

function pickup( plr )
    if getElementData(source,"kickeditem") and not isPedInVehicle(plr) then
		if getElementData(source,"name") == "papierosy" then
			exports["cmta-db"]:dbSet("update cmta_inventory set cigarettes=cigarettes+? where uid=?",getElementData(source,"amount"),getElementData(plr,"player:data").uid)
		end
		outputChatBox("#ccc000[Tajemnicza Paczka] #808080Podniesiono #ff0000"..getElementData(source,"amount").."x "..getElementData(source,"name")..".",plr,255,255,255,true)
		destroyElement(source)
	end
end
addEventHandler( "onPickupHit", resourceRoot, pickup )